package cn.js.sip.rat.task.controller;

import cn.js.sip.rat.system.controller.BaseController;
import cn.js.sip.rat.system.dto.AjaxResult;
import cn.js.sip.rat.system.page.TableDataInfo;
import cn.js.sip.rat.task.entity.JobInfoEntity;
import cn.js.sip.rat.task.service.TaskService;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: cn.js.sip.rat.task
 * @Description: TODO
 * @author: xuxiaoli
 * @Date: 2020-01-16   9:32
 */
@Controller
@RequestMapping("system/job")
public class TaskController extends BaseController {
    private String prefix = "monitor/job";

    @Resource(name = "taskService")
    private TaskService taskService;
    @GetMapping
    public String index() {
        return prefix + "/job";
    }

    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(JobInfoEntity job) {
        return taskService.query(job);
    }
    @GetMapping("/detail/{id}")
    public String detail(@PathVariable("id")String id, Map<String,Object> map) {
        JobInfoEntity entity = taskService.findById(id);
        map.put("job",entity);
        return prefix+"/detail";
    }

    //    @RequiresPermissions("monitor:job:list")
    @PostMapping("/run")
    @ResponseBody
    public AjaxResult run(JobInfoEntity job) {
        taskService.run(job);
        return AjaxResult.success();
    }

    @GetMapping("/add")
    public String add() {
        return prefix+"/add";
    }
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id")String id, Map<String,Object> map) {
        JobInfoEntity entity = taskService.findById(id);
        map.put("job",entity);
        return prefix+"/edit";
    }
    /**
     * 新增保存调度
     */
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated JobInfoEntity job)  {
        taskService.save(job);
        return AjaxResult.success();
    }
    /**
     * 新增保存调度
     */
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult edit(@Validated JobInfoEntity job)  {
        taskService.update(job);
        return AjaxResult.success();
    }
    /**
     * 任务调度状态修改
     */
    @PostMapping("/changeStatus")
    @ResponseBody
    public AjaxResult changeStatus(JobInfoEntity job) {
        JobInfoEntity newJob = taskService.findById(job.getId());
        newJob.setStatus(job.getStatus());
        taskService.changeStatus(newJob);
        return AjaxResult.success();
    }

    /**
     * 新增保存调度
     */
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(@RequestParam("ids") List<String> ids)  {
        return taskService.deleteByIds(ids);
    }

    /**
     * 校验cron表达式是否有效
     */
    @PostMapping("/checkCronExpressionIsValid")
    @ResponseBody
    public boolean checkCronExpressionIsValid(JobInfoEntity job) {
        return taskService.checkCronExpressionIsValid(job.getCronExpression());
    }
}
